<?php

class KitAttachmentModel
{
    public function addKitAttachment($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']) ||  empty($kitInfo['attachment'][0]))
        {
            AC::$errorNo    =   'KAM001';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $data['uid']        =   $kitInfo['uid'];
        $data['bodyId']     =   $kitInfo['bodyId'];
        $data['uploadTime'] =   AC::SERVER_TIME;

        foreach ($kitInfo['attachment']   as  $path)
        {
            if (empty($path))   continue;
            $data['attachmentPath'] =   $path;

            Storage::addData($key, $data);
        }
        return  TRUE;
    }/*}}}*/

    public function getKitAttachment($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KAM002';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $where  =   array('bodyId' => $kitInfo['bodyId']);
        $result =   Storage::getConditionManyData($key, $where);
        return  $result;
    }/*}}}*/

    public function setKitAttachment($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KAM003';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $data['uploadTime'] =   AC::SERVER_TIME;
        $data['status']     =   1;

        # No Attachment ! delete all!   /*{{{*/
        if (empty($kitInfo['attachment'][0]))
        {
            $where  =   array('bodyId' => $kitInfo['bodyId']);
            $result =   Storage::setConditionData($key, $data, $where);
            if (empty($result)) AC::$errorNo    =   'KAM004';

            return  TRUE;
        }/*}}}*/

        $oldAttachement =   $this->getKitAttachment($kitInfo);
        # old attachment is null, so ,all insert    /*{{{*/
        if (empty($oldAttachement))
        {
            return  $this->addKitAttachment($kitInfo);
        }/*}}}*/

        $transArray =   array_flip($kitInfo['attachment']);
        $tmpArray   =   array();
        foreach ($oldAttachement    as  $attachmentInfo)
        {
            if (isset($transArray[$attachmentInfo['attachmentPath']]))
            {
                $tmpArray[] =   $attachmentInfo['attachmentPath'];
                unset($transArray[$attachmentInfo['attachmentPath']]);
            }
        }

        $oldAttachementNum  =   count($oldAttachement);
        $tmpArrayNum        =   count($tmpArray);
        $newAttachementNum  =   count($kitInfo['attachment']);
        # no find repeat! /*{{{*/
        if (empty($tmpArrayNum))
        {
            # delete all
            $where  =   array('bodyId' => $kitInfo['bodyId']);
            $result =   Storage::setConditionData($key, $data, $where);

            # add new
            return  $this->addKitAttachment($kitInfo);
        }/*}}}*/

        # No new attachment, and no delete attachment   /*{{{*/
        if (($tmpArrayNum   ==  $oldAttachementNum) && ($tmpArrayNum == $newAttachementNum))
        {
            return  TRUE;
        }/*}}}*/

        # delete part attachment/*{{{*/
        if ($tmpArrayNum    !=  $oldAttachementNum)
        {
            $pathString =   "('". implode("', '", $kitInfo['attachment']). "')";
            $where      =   ' bodyId = "'. $kitInfo['bodyId']. '" AND attachmentPath NOT IN '. $pathString;
            $result     =   Storage::setConditionData($key, $data, $where);
        }/*}}}*/

        # add part attachment/*{{{*/
        if (count($transArray)  >   0)
        {
            $newAttachment  =   array_keys($transArray);
            $kitInfo['attachment']    =   $newAttachment;
            return  $this->addKitAttachment($kitInfo);
        }/*}}}*/

        return  TRUE;
    }/*}}}*/

    public function removeKitAttachment($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KAM005';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $data['uploadTime'] =   AC::SERVER_TIME;
        $data['status']     =   1;

        $where  =   array('bodyId' => $kitInfo['bodyId']);
        $result =   Storage::setConditionData($key, $data, $where);
        if (empty($result)) AC::$errorNo    =   'KAM006';

        return  TRUE;
    }/*}}}*/

    private function getKitKeyPrefix($kitType, $id)/*{{{*/
    {
        $kitTypeArray   =   array(
                                AC::FEED_KIT_ANNIVERSARY    =>  'KAA/',
                                AC::FEED_KIT_BMI            =>  'KBA/',
                                AC::FEED_KIT_MENSES         =>  'KMA/',
                                AC::FEED_KIT_NOTE           =>  'KNA/',
                                AC::FEED_KIT_SCHEDULE       =>  'KSA/',
                                AC::FEED_KIT_TALLYBOOK      =>  'KTA/',
                            );

        if (empty($kitTypeArray[$kitType]))
        {
            AC::$errorNo    =   'KAM007';
            return  FALSE;
        }
        $key    =   $kitTypeArray[$kitType]. $id;
        return  $key;
    }/*}}}*/

}


